﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;
using System.ComponentModel;
namespace EAGL.Core
{
    public static class GUIDManager
    {
        /// <summary>
        /// Searches the system registeru for the GUID assigned to the supplied interface.
        /// </summary>
        /// <param name="interfaceName">The name of the interface you want to get a Guid from</param>
        /// <returns>Guid</returns>
        /// 

        // to-do: try add a casche 
        public static Guid GetGUIDForInterface(string interfaceName)
        {
            RegistryKey crKey = Registry.ClassesRoot;
            RegistryKey interfaces;
            try
            {
                interfaces = crKey.OpenSubKey("Interface");
                string[] keys = interfaces.GetSubKeyNames();
                int c = keys.Length;
                int i = 0;
                bool found = false;
                string keyName = "";
                RegistryKey k;
                while (i < c && !found)
                {
                    keyName = keys[i];
                    k = interfaces.OpenSubKey(keyName);
                    //string[] keyValues = k.GetValueNames();
                    string keyValue = k.GetValue("") as string;
                    found = keyValue == interfaceName;
                    i++;
                    k.Close();
                }
                if (found)
                    return new Guid(keyName);
            }
            catch (Exception ex)
            {
                throw new Exception("Can't open interface key");
            }
            finally
            {
                crKey.Close();
            }
            return Guid.Empty;           
        }
 
    }
}
